package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.OilCardInfoModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.observable.ObservableListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的设备
 * Created by Hq on 2017/7/12.
 */
public class MyDeviceAty extends BaseCommAty {

    @BindView(R.id.id_my_device_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_my_device_listv)
    ObservableListView mObserListv;
    private CommonAdapter<OilCardInfoModel> mAdapter;
    List<OilCardInfoModel> mOilCards = new ArrayList<OilCardInfoModel>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_mydevice, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mOilCards.isEmpty();
    }

    @Override
    public void initView() {
        setTitleText("我的设备");
        setXRefreshView(mXRefreshView, true, false, false, false, new SmileyHeaderView(aty), null);
        mAdapter = new CommonAdapter<OilCardInfoModel>(aty, mOilCards, R.layout.adp_my_device) {
            @Override
            public void convert(final ViewHolder helper, final OilCardInfoModel item) {
                if (null == item) {
                    return;
                }
                if (StringUtils.isEmpty(item.getPlateNumber())) {
                    helper.setText(R.id.id_device_platem, "暂未关联司机");
                } else {
                    helper.setText(R.id.id_device_platem, item.getPlateNumber());
                }
                helper.setText(R.id.id_device_card_num, "卡号：" + StringUtils.replaceBankString(item.getCardNum(), 0));
                helper.setText(R.id.id_device_company_txt, item.getOrgName());
                helper.setText(R.id.id_device_tel_txt, item.getCardTel());
                helper.getView(R.id.id_device_tel_txt).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!StringUtils.isEmpty(item.getCardTel())) {
                            StringUtils.telPhone(aty, item.getCardTel());
                        }
                    }
                });
                helper.getView(R.id.id_look_exchange_txt).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //查看交易流水
//                        Intent intent = new Intent(aty, LookMyTradeAty.class);
                        Intent intent = new Intent(aty, LookMyBillAty.class);
                        intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_DEVICE);
                        intent.putExtra(IntentKey.General.KEY_ID, item.getUid());
                        showActivity(aty, intent);
                    }
                });
            }
        };
        mObserListv.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        loadData(true);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false);
            }
        });
    }

    /**
     * 加载数据
     */
    public void loadData(boolean isShLowoad) {
        ((OilMealPresenter) presenter).getMyOilCards(isShLowoad, "0", "", "", "", "", "");
        if (!isShLowoad) {
            stopRefresh(mXRefreshView, 2000);
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        mXRefreshView.stopRefresh();
        if (null != t) {
            if (TextUtils.equals(ConstantTag.MY_OIL_CARDS, tag)) {
                //获得油卡列表成功
                List<OilCardInfoModel> oilCards = (List<OilCardInfoModel>) t;

                mOilCards.clear();
                mOilCards.addAll(oilCards);
                mAdapter.notifyDataSetChanged();
            } else if (TextUtils.equals(ConstantTag.DATA_SUCCESS_NULL, tag)) {
                //没有油卡了
                mOilCards.clear();
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}
